package com.dense.kuiniu.floatcabinet.utils.timer;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;

import com.dense.kuiniu.floatcabinet.R;

import java.util.Locale;


/**
 * @author Administrator
 * @date 2020/8/7
 */
public class CountDownView extends AppCompatTextView {
    private CounterController mController;
    private int mSeconds;
    private String str;
    private CounterController.OnCompleteListener mOnCompleteListener;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            setText((String) msg.obj);
        }
    };

    public CountDownView(Context context) {
        this(context, null);
    }

    public CountDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CountDownView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);

    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CountDownView);
        try {
            mSeconds = ta.getInt(R.styleable.CountDownView_cd_second, 30);
            str = ta.getString(R.styleable.CountDownView_cd_str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mController = new CounterController(mSeconds);

        ta.recycle();
    }

    public void start(CounterController.OnCompleteListener listener) {
        mOnCompleteListener = listener;
        mController.setCounterListener(new CounterController.OnCounterListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void onCount(long time, int counter) {
                String msg = String.format(Locale.CHINA,
                        "%d %s", time - counter, str);
                Message message = Message.obtain();
                message.obj = msg;
                mHandler.sendMessage(message);
            }

            @Override
            public void onPause() {

            }

            @Override
            public void onResume() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onComplete() {
                if (mOnCompleteListener != null) {
                    mOnCompleteListener.onComplete();
                }
            }
        });
        mController.start();
    }

    public void resume() {
        if (mController != null) {
            mController.resume();
        }
    }

    public void pause() {
        if (mController != null) {
            mController.pause();
        }
    }

    public void stop() {
        if (mController != null) {
            mController.stop();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mController != null) {
            mController.stop();
        }
    }


}